package com.huilian.iotbox.admin.wap.service.impl;

import com.huilian.iotbox.admin.wap.dao.PermissionGroupItemDao;
import com.huilian.iotbox.admin.wap.service.MerchantStoreService;
import com.huilian.iotbox.admin.wap.service.PermissionGroupDeviceTypeService;
import com.huilian.iotbox.admin.wap.service.PermissionGroupItemService;
import com.huilian.iotbox.data.common.ServerResponse;
import com.huilian.iotbox.data.dto.PermissionGroupItemDto;
import com.huilian.iotbox.data.enums.PermissionGroupIDEnum;
import com.huilian.iotbox.data.enums.RoleEnum;
import com.huilian.iotbox.data.po.ModuleDeviceType;
import com.huilian.iotbox.data.po.PermissionGroupDeviceType;
import com.huilian.iotbox.data.vo.ModuleVo;
import com.huilian.iotbox.data.vo.PermissionGroupItemVo;
import lombok.Getter;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.ObjectUtils;

import java.util.*;
import java.util.stream.Collectors;

/**
 * @author LinYinDong
 * @date 2024/10/29 下午4:06
 */
@Service
public class PermissionGroupItemServiceImpl implements PermissionGroupItemService {
    @Autowired
    private PermissionGroupItemDao permissionGroupItemDao;
    @Autowired
    private MerchantStoreService merchantStoreService;
    @Autowired
    private PermissionGroupDeviceTypeService permissionGroupDeviceTypeService;
    @Getter
    public enum BranchIdEnum {
        HOME(1, "查询首页菜单"),
        MALL(2, "查询积分商城"),
        MY(3, "查询我的");
        //枚举值所包含的属性
        private Integer id;
        private String desc;
        //构造方法
        BranchIdEnum(Integer id, String desc) {
            this.id = id;
            this.desc = desc;
        }
    }
    /**
     * 首页菜单
     *      默认16,17,18（活动，经营，其他）
     *      13, 14, 15 (游乐设备，售后机设备，拍照设备)根据用户有没有绑定来追加显示
     *      没有绑定设备，查询全部菜单13, 14, 15,16,17,18（游乐设备，售后机设备，拍照设备，活动，经营，其他）
     * 我的菜单
     *     20，21，22（资产管理，品牌管理，我的工具）
     * */
    @Override
    public ServerResponse findByPermissionGroupId(PermissionGroupItemDto permissionGroupItemDto) {
        if(ObjectUtils.isEmpty(permissionGroupItemDto.getTypes())){
            return ServerResponse.createByErrorMessage("缺少参数");
        }
        if(ObjectUtils.isEmpty(permissionGroupItemDto.getQueryBranch())){
            return ServerResponse.createByErrorMessage("查询分支不能为空");
        }
        List<PermissionGroupItemVo> list = new ArrayList<>();
        PermissionGroupItemVo permissionGroupItemVo = new PermissionGroupItemVo();
        Map<Integer, List<PermissionGroupItemVo>> moduleMap = new HashMap<>();

        //首页根据菜单显示
        if(BranchIdEnum.HOME.getId().compareTo(permissionGroupItemDto.getQueryBranch()) == 0){
            //查询是否绑定过设备
            List<PermissionGroupDeviceType> permissionGroupDeviceTypes;
            //查询场地
            Integer[] allStoreId = merchantStoreService.findAllMerchantStoreIdByUserId(permissionGroupItemDto.getUserInfo());
            if(allStoreId.length == 0){
                permissionGroupDeviceTypes = new ArrayList<>();
            }else{
                permissionGroupItemDto.setAllStoreId(allStoreId);
                permissionGroupDeviceTypes = permissionGroupDeviceTypeService.findPermissionGroupDeviceTypeList(allStoreId);
            }
            //根据绑定的设备来显示菜单
            if(permissionGroupDeviceTypes.size() > 0){
                permissionGroupDeviceTypes.forEach(item -> {
                    permissionGroupItemDto.getTypes().add(item.getPermissionGroupId());
                });
            }else{
                //没有绑定设备，显示全部设备
                permissionGroupItemDto.getTypes().add(PermissionGroupIDEnum.PLAY_DEVICE.getCode());
                permissionGroupItemDto.getTypes().add(PermissionGroupIDEnum.VENDING_DEVICE.getCode());
                permissionGroupItemDto.getTypes().add(PermissionGroupIDEnum.PHOTOGRAPH_DEVICE.getCode());
            }
        }

        if(permissionGroupItemDto.getUserInfo().getRoleId().equals(RoleEnum.SUPER_ADMINISTRATOR.getRoleId())
                || permissionGroupItemDto.getUserInfo().getRoleId().equals(RoleEnum.AGENT.getRoleId())
                || permissionGroupItemDto.getUserInfo().getRoleId().equals(RoleEnum.MERCHANT.getRoleId())){
            list = permissionGroupItemDao.findByPermissionGroupIdAndRole(permissionGroupItemDto);
        }else if(permissionGroupItemDto.getUserInfo().getRoleId().equals(RoleEnum.SUB_ACCOUNT.getRoleId())){
            list = permissionGroupItemDao.findByPermissionGroupIdAndUser(permissionGroupItemDto);
        }
        for (PermissionGroupItemVo moduleVo : list) {
            List<PermissionGroupItemVo> moduleListByType = moduleMap.getOrDefault(moduleVo.getPermissionGroupId(), new ArrayList<>());
            moduleListByType.add(moduleVo);
            moduleMap.put(moduleVo.getPermissionGroupId(), moduleListByType);
        }
        if(BranchIdEnum.HOME.getId().compareTo(permissionGroupItemDto.getQueryBranch()) == 0){
            permissionGroupItemVo.setHostModuleList(moduleMap.getOrDefault(PermissionGroupIDEnum.MANAGEMENT.getCode(), new ArrayList<>()));
            permissionGroupItemVo.setPlayModuleList(moduleMap.getOrDefault(PermissionGroupIDEnum.PLAY_DEVICE.getCode(), new ArrayList<>()));
            permissionGroupItemVo.setActivityModuleList(moduleMap.getOrDefault(PermissionGroupIDEnum.STORE_ACTIVITY.getCode(), new ArrayList<>()));
            permissionGroupItemVo.setSalesModuleList(moduleMap.getOrDefault(PermissionGroupIDEnum.VENDING_DEVICE.getCode(), new ArrayList<>()));
            permissionGroupItemVo.setOtherModuleList(moduleMap.getOrDefault(PermissionGroupIDEnum.OTHER.getCode(), new ArrayList<>()));
            permissionGroupItemVo.setPhotographModuleList(moduleMap.getOrDefault(PermissionGroupIDEnum.PHOTOGRAPH_DEVICE.getCode(), new ArrayList<>()));
        }else if (BranchIdEnum.MALL.getId().compareTo(permissionGroupItemDto.getQueryBranch()) == 0){
            //商城
            permissionGroupItemVo.setMallModuleList(moduleMap.getOrDefault(PermissionGroupIDEnum.SHOPPING_MALL.getCode(), new ArrayList<>()));
        }else if (BranchIdEnum.MY.getId().compareTo(permissionGroupItemDto.getQueryBranch()) == 0){
            permissionGroupItemVo.setAssetsMenuList(moduleMap.getOrDefault(PermissionGroupIDEnum.ASSETS.getCode(), new ArrayList<>()));
            permissionGroupItemVo.setBrandMenuList(moduleMap.getOrDefault(PermissionGroupIDEnum.BRAND.getCode(), new ArrayList<>()));
            permissionGroupItemVo.setToolMenuList(moduleMap.getOrDefault(PermissionGroupIDEnum.TOOL.getCode(), new ArrayList<>()));
        }
        return ServerResponse.createBySuccess(permissionGroupItemVo);
    }
}
